package kov.core.body.zy
{
	import com.riaoo.events.AnimationEvent;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.events.Event;
	
	import kov.core.BodyState;
	import kov.core.IBody;
	import kov.core.StateCommand;
	import kov.core.body.StateEnum;
	import kov.core.global.Global;
	import kov.core.global.KeyConfig;
	
	public class ZYTiao extends BodyState
	{
		[Embed("../../../../../assets/zy/tiao/0.gif")]
		private var Frame0:Class;
		[Embed("../../../../../assets/zy/tiao/1.gif")]
		private var Frame1:Class;
		[Embed("../../../../../assets/zy/tiao/2.gif")]
		private var Frame2:Class;
		[Embed("../../../../../assets/zy/tiao/3.gif")]
		private var Frame3:Class;
		[Embed("../../../../../assets/zy/tiao/4.gif")]
		private var Frame4:Class;
		[Embed("../../../../../assets/zy/tiao/5.gif")]
		private var Frame5:Class;
		[Embed("../../../../../assets/zy/tiao/6.gif")]
		private var Frame6:Class;
		[Embed("../../../../../assets/zy/tiao/7.gif")]
		private var Frame7:Class;
		[Embed("../../../../../assets/zy/tiao/8.gif")]
		private var Frame8:Class;
		[Embed("../../../../../assets/zy/tiao/9.gif")]
		private var Frame9:Class;
		[Embed("../../../../../assets/zy/tiao/10.gif")]
		private var Frame10:Class;
		[Embed("../../../../../assets/zy/tiao/11.gif")]
		private var Frame11:Class;
		[Embed("../../../../../assets/zy/tiao/12.gif")]
		private var Frame12:Class;
		
		public function ZYTiao(body:IBody, priority:int=1)
		{
			super(StateEnum.TIAO, body, priority);
			createRenderer();
		}
		
		// 创建渲染帧。
		private function createRenderer():void
		{
			var f0:BitmapData = (new Frame0() as Bitmap).bitmapData;
			var f1:BitmapData = (new Frame1() as Bitmap).bitmapData;
			var f2:BitmapData = (new Frame2() as Bitmap).bitmapData;
			var f3:BitmapData = (new Frame3() as Bitmap).bitmapData;
			var f4:BitmapData = (new Frame4() as Bitmap).bitmapData;
			var f5:BitmapData = (new Frame5() as Bitmap).bitmapData;
			var f6:BitmapData = (new Frame6() as Bitmap).bitmapData;
			var f7:BitmapData = (new Frame7() as Bitmap).bitmapData;
			var f8:BitmapData = (new Frame8() as Bitmap).bitmapData;
			var f9:BitmapData = (new Frame9() as Bitmap).bitmapData;
			var f10:BitmapData = (new Frame10() as Bitmap).bitmapData;
			var f11:BitmapData = (new Frame11() as Bitmap).bitmapData;
			var f12:BitmapData = (new Frame12() as Bitmap).bitmapData;
			this.renderer.push(f0);
			this.renderer.push(f1);
			this.renderer.push(f2);
			this.renderer.push(f3);
			this.renderer.push(f4);
			this.renderer.push(f5);
			this.renderer.push(f6);
			this.renderer.push(f7);
			this.renderer.push(f8);
			this.renderer.push(f9);
			this.renderer.push(f10);
			this.renderer.push(f12);
		}
		
		private var speedX:int; // 水平移动速度
		override public function execute():void
		{
			this.body.renderer.frameSequence = this.renderer;
			this.body.renderer.delay = 80;
			this.body.renderer.offsetX = 74;
			this.body.renderer.offsetY = 155;
			this.body.renderer.gotoAndPlay(0);
			
			this.body.renderer.addEventListener(AnimationEvent.ANIMATION_END, onAnimationEnd);
			
			var isLeft:Boolean = Global.key.isDown(KeyConfig.LEFT);
			var isRight:Boolean = Global.key.isDown(KeyConfig.RIGHT);
			if ( !(isLeft || isRight) || (isLeft && isRight) )
			{
				return;
			}
			
			this.speedX = isLeft ? -2 : 2;
			this.body.renderer.addFrameScript(1, startLoop);
			this.body.renderer.addFrameScript(8, stopLoop);
		}
		
		override public function exit():void
		{
			this.body.renderer.removeEventListener(AnimationEvent.ANIMATION_END, onAnimationEnd);
		}
		
		private function onAnimationEnd(event:AnimationEvent):void
		{
			var cmd:StateCommand = new StateCommand(StateEnum.XING_ZOU);
			this.body.executeStateCommand(cmd);
		}
		
		private function loop(event:Event):void
		{
			var toX:Number = this.body.x + this.speedX;
			if ( !Global.world.mapData.blockData.isBlock(toX, this.body.y) )
			{
				this.body.x += this.speedX;
			}
		}
		
		private function startLoop():void
		{
			this.body.renderer.removeFrameScript(1);
			Global.stage.addEventListener(Event.ENTER_FRAME, loop);
		}
		
		private function stopLoop():void
		{
			this.body.renderer.removeFrameScript(8);
			Global.stage.removeEventListener(Event.ENTER_FRAME, loop);
		}
		
	}
}